function deepClone(obj) {
  var copy;
  switch (typeof obj) {
    case "undefined":
      break;
    case "number":
      copy = obj - 0;
      break;
    case "string":
      copy = obj + "";
      break;
    case "boolean":
      copy = obj;
      break;
    case "object":
      if (obj === null) {
        copy = null;
      } else {
        if (obj instanceof Array) {
          copy = [];
          for (var i = 0; i < obj.length; i++) {
            copy.push(clone(obj[i]));
          }
        } else {
          copy = {};
          for (var j in obj) {
            copy[j] = clone(obj[j]);
          }
        }
      }
      break;
    default:
      copy = obj;
      break;
  }
  return copy;
}

export function dateFormat(date, fmt) {
  var o = {
    "M+": date.getMonth() + 1, //月份   
    "d+": date.getDate(), //日   
    "h+": date.getHours(), //小时   
    "m+": date.getMinutes(), //分   
    "s+": date.getSeconds(), //秒   
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
    "S": date.getMilliseconds() //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}


export function debounce(fn, wait) {
  let timer = null;
  return function (...args) {
    let _args = args;
    let context = this;
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.call(context, _args);
    }, wait);
  }
}

export function throttle(fn, wait) {
  let timer, lasttime; //lasttime 上次执行的时间
  return function (...args) {
    let _args = args;
    let context = this;

    let now = +new Date(); //本次触发时间gettime

    if (!lasttime && now >= lasttime + wait) { //第一次执行 && 超过了指定wait时间
      lasttime = now; //设置最新执行时间
      fn.call(context, _args);
    } else { //没到等待时间 重新设置定时器=剩余等待时间
      clearTimeout(timer);
      timer = setTimeout(() => {
        lasttime = now; //定时器执行时设置执行时间
        fn.call(context, _args);
      }, (wait - (now - lasttime)));
    }

  }
}

